/**
 * @file   1135.cpp
 * @author Shuang Hu <hsmath@ubuntu>
 * @date   Sun Mar  7 22:35:35 2021
 * 
 * @brief  A brief program to check if a tree is a RBTree
 * 
 * 
 */

#include <iostream>
#include <cmath>
using namespace std;
int flag1=1;
int flag2=1; 
class RBTree{
private:
    class TNode{
    public:
	int color;//0 means red and 1 means black
	int data;
	int bh;//mark the blackheight
	TNode* Left;
	TNode* Right;
    };
    TNode* Root;
    int nodenum;
public:
    RBTree(){
	Root=NULL;
    }
    RBTree(int _nodenum){
	nodenum=_nodenum;
	Root=NULL;
    }
    void getnodenum(int _nodenum){
	nodenum=_nodenum;
    }
    int max(int a,int b){
	if(a>b){
	    return a;
	}else{
	    return b;
	}
    }
    TNode* insert(int _data,TNode* TN);//insert some elements
    int checkroot();//check the root node
    int getbh(TNode* TN);
    void checkbh(TNode* TN);//check the blackheight
    void checkred(TNode* TN);//check the condition 2
    void check();//check if the Tree is a RBTree
    void insert(int _data){//for main function
	insert(_data,Root);
    }
};
RBTree::TNode* RBTree::insert(int _data,TNode* TN){
    if(Root==NULL){//Empty Tree!
	Root=new TNode;
	Root->data=abs(_data);
	if(_data>0){
	    Root->color=1;
	}else{
	    Root->color=0;
	}
	Root->Left=NULL;
	Root->Right=NULL;
	if(Root->color==0){//Root is a red node, blackheight is 1
	    Root->bh=1;
	}else{//Root is a black node, blackheight is 2
	    Root->bh=2;
	}
    }
    else{
	if(TN==NULL){//case1:the Tree elements is a leaf node
	    TN=new TNode;
	    TN->data=abs(_data);
	    if(_data>0){
		TN->color=1;
	    }else{
		TN->color=0;
	    }
	    TN->Left=NULL;
	    TN->Right=NULL;
	    if(TN->color==0){
		TN->bh=1;
	    }else{
		TN->bh=2;
	    }
	}else if(abs(_data)>TN->data){
	    TN->Right=insert(_data,TN->Right);
	    TN->bh=max(getbh(TN->Left),getbh(TN->Right));
	    if(TN->color==1){//Black
		TN->bh++;
	    }
	}else{
	    TN->Left=insert(_data,TN->Left);
	    TN->bh=max(getbh(TN->Left),getbh(TN->Right));
	    if(TN->color==1){//Black
		TN->bh++;
	    }
	}
    }
    return TN;
}
int RBTree::checkroot(){
    int flag=1;
    if(Root->color==0){//Root is red
	flag=0;
    }
    return flag;
}
int RBTree::getbh(TNode* TN){
    if(TN==NULL){
	return 1;//The blackheight of NIL
    }
    else{
	return TN->bh;
    }
}
void RBTree::checkbh(TNode* TN){
    if(TN==NULL){
	//do nothing
    }else{
	if(getbh(TN->Left)!=getbh(TN->Right)){
	    flag1=0;
	}
	checkbh(TN->Left);
	checkbh(TN->Right);
    }
}
void RBTree::checkred(TNode* TN){
    if(TN==NULL){
	//do nothing
    }else{
	if(TN->color==0){
	    if(TN->Left){
		if(TN->Left->color==0){
		    flag2=0;
		}
	    }
	    if(TN->Right){
		if(TN->Right->color==0){
		    flag2=0;
		}
	    }
	}
	checkred(TN->Left);
	checkred(TN->Right);
    }
}
void RBTree::check(){
    int flag=1;
    flag1=1;
    flag2=1;
    if(checkroot()==0){
	flag=0;
    }
    checkbh(Root);
    checkred(Root);
    if(flag1==0||flag2==0){
    	flag=0;
	}
    if(flag==0){
	cout<<"No"<<endl;
    }else{
	cout<<"Yes"<<endl;
    }
}
int main(){
    int checknum;
    cin>>checknum;
    RBTree RBT[checknum];
    for(int i=0;i<checknum;i++){
	int num;
	int data1;
	cin>>num;
	RBT[i].getnodenum(num);
	for(int j=0;j<num;j++){
	    cin>>data1;
	    RBT[i].insert(data1);
	}
	RBT[i].check();
    }
}
